﻿using System;
using System.Collections.Generic;
using System.Linq;
using Shell.DomainDot;
using Framework.Poco;

namespace Framework.Domain
{
    /// <summary>
    /// 经销商分组下的友情链接
    /// </summary>
    public class CategoryFriendlyLinkContext : DomainDotBase<Category>
    {
        /// <summary>
        /// 属于经销商分组下的友情链接
        /// </summary>
        public WithMany<FriendlyLink> FriendlyLinks { private get; set; }

        /// <summary>
        /// 获取所有经销商分组下的友情链接
        /// </summary>
        public IEnumerable<FriendlyLink> GetFriendlyLinks()
        {
            return this.FriendlyLinks;
        }

        /// <summary>
        /// 友情链接创建
        /// </summary>
        public void Create(FriendlyLink friendlyLink)
        {
            friendlyLink.Created = DateTime.Now;
            friendlyLink.CategoryOrAgencyGroup = this.Instance.Name;
            var entity = this.FriendlyLinks.Create();
            PocoUtils.AssignProperty<FriendlyLink>(friendlyLink, entity, typeof(Guid));
            this.FriendlyLinks.Submit();
        }

        /// <summary>
        /// 友情链接删除
        /// </summary>
        public void Delete(Guid identity)
        {
            var original = this.GetFriendlyLinks().SingleOrDefault(o => o.Id == identity);
            if (original != null)
            {
                this.FriendlyLinks.Delete(original.Id);
                this.FriendlyLinks.Submit();
            }
        }
    }
}
